package com.pm.service.official.impl;

import com.pm.entity.po.official.OfficialMeanDevRank;
import com.pm.entity.po.official.OfficialParamPo;
import com.pm.entity.vo.official.OfficialCommonVo;
import com.pm.mapper.official.OfficialMeanDevRankMapper;
import com.pm.service.impl.BaseServiceImpl;
import com.pm.service.official.OfficialMeanDevRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

/**
 * @program: pm_info_api
 * @description:党政一把手离均差排名
 * @author: ht
 * @create: 2020-08-26 17:46
 **/
@Service
public class OfficialMeanDevRankServiceImpl extends BaseServiceImpl<OfficialMeanDevRank> implements OfficialMeanDevRankService {

    @Autowired
    OfficialMeanDevRankMapper officialMeanDevRankMapper;

    @Resource
    private void setBaseMapper(OfficialMeanDevRankMapper officialMeanDevRankMapper) {
        super.setBaseMapper(officialMeanDevRankMapper);
    }

    @Override
    public Integer addOne(OfficialMeanDevRank officialMeanDevRank) {
        return officialMeanDevRankMapper.addOne(officialMeanDevRank);
    }

    @Override
    public List<OfficialMeanDevRank> getList(OfficialCommonVo officialCommonVo) {
        return officialMeanDevRankMapper.getList(officialCommonVo);
    }

    @Override
    public int getUserNumByYearAndTimeNode(String year, String timeNode) {
        return officialMeanDevRankMapper.getUserNumByYearAndTimeNode(year, timeNode);
    }

    @Override
    public int updateRankByMeanDevSum(OfficialParamPo officialParamPo) {
        List<OfficialMeanDevRank> meanDevRankList = officialMeanDevRankMapper.getMeanDevSum(officialParamPo);
        meanDevRankList.sort(Comparator.comparing(OfficialMeanDevRank::getMeanDevSum).reversed());
        int rank = 0, index = 0;
        OfficialMeanDevRank officialMeanDevRank = null;
        for (int i = 0, j = meanDevRankList.size(); i < j; i++) {
            index++;
            officialMeanDevRank = meanDevRankList.get(i);
            if (i == 0) {
                rank++;
            } else if (officialMeanDevRank.getMeanDevSum() - meanDevRankList.get(i - 1).getMeanDevSum() == 0) {
                rank = meanDevRankList.get(i - 1).getRank();
            } else {
                rank = index;
            }
            officialMeanDevRank.setRank(rank);
            meanDevRankList.set(i, officialMeanDevRank);
        }
        return officialMeanDevRankMapper.batchUpdate(meanDevRankList);
    }

    @Override
    public Integer removeOne(OfficialMeanDevRank officialMeanDevRank) {
        return officialMeanDevRankMapper.removeOne(officialMeanDevRank.getId());
    }

}
